package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Order;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private HttpSession session;

    @PostMapping("/order/submit")
    public ResultInfo save(@RequestBody Order order) {
        orderService.save(order);
        return ResultInfo.success(null);
    }

    @GetMapping("/order/userPage")//查看订单、历史订单都是这个接口
    public ResultInfo findPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize
    ) {
        Page<Order> page = orderService.findPage(pageNum, pageSize);
        return ResultInfo.success(page);
    }

    @PostMapping("/user/logout")
    public ResultInfo logout() {
        //1销毁session
        session.invalidate();
        return ResultInfo.success(null);
    }
}
